﻿using System;
using System.Data;
using System.Drawing;
using UniversityStudentManagemnetSystem.BLL;
using UniversityStudentManagemnetSystem.DAO.DAL;

namespace UniversityStudentManagemnetSystem.UI
{
    public partial class DeapartmentPage : System.Web.UI.Page
    {
        private DepartmentManager departmentManager = new DepartmentManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/Account/Login.aspx", false);
            }
            
                PopulatedGridView();
         
            
        }

        private void PopulatedGridView()
        {
            try
            {
                DataTable departments = departmentManager.GetDepartments();
                departmentGridView.DataSource = departments;
                departmentGridView.DataBind();
            }
            catch (Exception exception)
            {

                messageLabel.Text = exception.Message;
            }

        }

        protected void deptSaveButton_Click(object sender, EventArgs e)
        {
            if(codeTextBox.Text!="" && nameTextBox.Text!="")
            {
                try
                {
                    string code = codeTextBox.Text;
                    string name = nameTextBox.Text;
                    Department department = new Department(code, name);
                    if (departmentManager.DepartmentSave(department))
                    {
                        messageLabel.ForeColor = Color.Green;
                        messageLabel.Text = "Saved successfully";
                    }
                }
                    catch(FormatException exception)
                    {
                        messageLabel.ForeColor = Color.Red;
                        messageLabel.Text = exception.Message;
                    }
                catch (Exception exception)
                {
                    messageLabel.ForeColor = Color.Red;
                    messageLabel.Text = exception.Message;
                }

               
            }


         else
            {
                messageLabel.ForeColor = Color.BlueViolet;
                messageLabel.Text = "Please fill up the all fields.";
            }


            PopulatedGridView();
            codeTextBox.Text = string.Empty;
            nameTextBox.Text = string.Empty;
        }

        protected void departmentGridView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}